package org.pisces.wechat.config;

import java.util.ArrayList;
import java.util.List;

import javax.sql.DataSource;

import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.boot.autoconfigure.MybatisProperties;
import org.mybatis.spring.boot.autoconfigure.SpringBootVFS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.DefaultResourceLoader;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

/**
 * springboot集成mybatis的基本入口 1）创建数据源 2）创建SqlSessionFactory
 */
@Configuration
@EnableTransactionManagement
public class MyBatisConfiguration {
    @Autowired
    private MybatisProperties properties;
    @Autowired
    private DataSource dataSource;

    /**
     * 根据数据源创建SqlSessionFactory
     */
    @Bean
    public SqlSessionFactory sqlSessionFactory() throws Exception {
        MybatisSqlSessionFactoryBean factory = new MybatisSqlSessionFactoryBean();
//		UtSqlSessionFactoryBean factory = new UtSqlSessionFactoryBean();
        // logger.debug("dataSource: {}", dataSource);
        // logger.debug(ToStringBuilder.reflectionToString(dataSource));
        factory.setDataSource(dataSource);
        if (StringUtils.hasText(this.properties.getConfigLocation())) {
            factory.setConfigLocation(new DefaultResourceLoader().getResource(this.properties.getConfigLocation()));
        }
        org.apache.ibatis.session.Configuration configuration = this.properties.getConfiguration();
        if (configuration == null && !StringUtils.hasText(this.properties.getConfigLocation())) {
            configuration = new org.apache.ibatis.session.Configuration();
        }
        factory.setConfiguration(configuration);
        if (this.properties.getConfigurationProperties() != null) {
            factory.setConfigurationProperties(this.properties.getConfigurationProperties());
        }
        if (StringUtils.hasLength(this.properties.getTypeAliasesPackage())) {
            factory.setTypeAliasesPackage(this.properties.getTypeAliasesPackage());
        }
        if (StringUtils.hasLength(this.properties.getTypeHandlersPackage())) {
            factory.setTypeHandlersPackage(this.properties.getTypeHandlersPackage());
        }
        if (!ObjectUtils.isEmpty(this.properties.resolveMapperLocations())) {
            factory.setMapperLocations(this.properties.resolveMapperLocations());
        }
        // 分页插件
    //    factory.setPlugins(new Interceptor[]{pageHelper()});
        // 设置枚举默认处理器
        factory.setTypeHandlers(initMybatisDefaultTypeHander());
        factory.setVfs(SpringBootVFS.class);
        // return factory.buildSqlSessionFactory();
        return factory.getObject();
    }

    private List<MybatisSqlSessionFactoryBean.Handler> initMybatisDefaultTypeHander() {
        List<MybatisSqlSessionFactoryBean.Handler> utHandlers = new ArrayList<MybatisSqlSessionFactoryBean.Handler>();
        MybatisSqlSessionFactoryBean.Handler utHandler = new MybatisSqlSessionFactoryBean.Handler();
        utHandler.setScanPkg("org.pisces.wechat.enums");
        utHandler.setTypeHandlerClass(MyBatisEnumTypeHandler.class);
        utHandlers.add(utHandler);
        return utHandlers;
    }


}
